

import 'package:flutter/material.dart';
import 'package:untitled/pages/Getx_project/ShopClassBean.dart';
import 'package:untitled/pages/Getx_project/ShopController.dart';
import 'package:untitled/pages/Getx_project/ShopItemPage.dart';
import 'package:get/get.dart';

class ShopMainPage extends StatefulWidget {
  const ShopMainPage({Key? key}) : super(key: key);

  @override
  State<ShopMainPage> createState() => _ShopMainPageState();
}

class _ShopMainPageState extends State<ShopMainPage> with TickerProviderStateMixin{
  TabController? _tabController;

  ShopController _shopController = Get.put(ShopController());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 第一步 网络请求 获取分类数据
    _shopController.requestShopClass();
   
  }
  @override
  Widget build(BuildContext context) {
    return GetBuilder(builder: (ShopController controller){
      // 获取数据
      List<ShopClassBean> classList = controller.classBeanList;
      if(classList.length == 0){
        return Scaffold(
          appBar: AppBar(
            title: Text("商品页面"),
          ),
          body:Center(
            child: Text("加载中"),
          )
        );
      }
       _tabController = new TabController(length: classList.length, vsync: this);

       // Tab 
       List<Tab> tabList =[];
       // 子页面
       List<Widget> itemList = [];
      
       // 构建页面数据
       classList.forEach((element) {
         // 顶部标签
         tabList.add(Tab(text: element.title,));
         // 标签子页面
         itemList.add(ShopItemPage(element));
         
       });
      return Scaffold(
          appBar: AppBar(
          title: Text("商品页面"),
          bottom:buildTabBar(tabList),
        ),
          backgroundColor: Colors.white,
          body: buildTabBarView(itemList),
          floatingActionButton: FloatingActionButton.extended(
              onPressed: (){}, 
              label: GetBuilder(
                id: "card",
                builder: (ShopController controller){
                  return Text("${controller.cardList.length}");
                },
              ) 
              ),
    );

    });
    
  }

  buildTabBar(List<Tab>tabList){
    return TabBar(
      isScrollable: true,
      controller: _tabController,
      tabs: tabList,
    );
  }

  buildTabBarView(List<Widget>itemList){
    return TabBarView(
        controller: _tabController,
        children:itemList, 
        );
  }
}